home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Programmation / Gooey1.3.1 / C++ Templates / mmMain.c < prev    next >
C/C++ Source or Header  |  1994-07-29  |  33KB  |  1,017 lines

  1. $$Message Main, mm:mmMain$Prototype.name$.cp
  2. $$File mm:mmMain$Prototype.name$.cp
  3. /* mmMain$Prototype.name$ */
  4.  
  5. /*    
  6.     Program name:  mmMain$Prototype.name$  
  7.     Function:  This is the main module for this program.  
  8.     History: $Date$ Original by $Author$
  9.  
  10.    */
  11.  
  12. #include "mmCommon$Prototype.name$.h"            /* Common */
  13. #include "Common$Prototype.name$.h"                /* Common */
  14.  
  15.  
  16. Boolean        DoIt;                                /* Flag saying an event is ready */
  17. short        code;                                /* Determine event type */
  18. WindowPtr    whichWindow;                        /* See which window for event */
  19. long        mResult;                            /* Menu list and item selected values */
  20. short        theMenu,theItem;                    /* Menu list and item selected */
  21. Boolean        Is_A_Dialog;                        /* Flag for modless dialogs */
  22. short        charCode, itemHit;                    /* For modeless dialogs*/
  23. char        ch;                                    /* Key pressed in Ascii */
  24. Boolean        DoTheModelessEvent, CmdDown;        /* For modeless dialogs*/
  25. WindowPeek    thePeeked;                            /* For modeless dialogs*/
  26.  
  27.  
  28. /* MAIN entry point */
  29. void main(void);
  30.  
  31. /* ======================================================= */
  32. /* ======================================================= */
  33.  
  34. /* Routine: WNEIsImplemented */
  35. /* Purpose: See if the MultiFinder trap, WaitNextEvent, is available */
  36.  
  37. Boolean CmmApplication::IsWNEIsImplemented()    /* See if WaitNextEvent is available */
  38. {
  39. #define    WNETrapNumber    0xA860                    /* The expected trap number */
  40. #define    kGestaltTrapID    0xA1AD                    /* The expected trap number */
  41.  
  42. SysEnvRec    theWorld;                            /* Environment record */
  43. OSErr        discardError;                        /* Error code returned */
  44. Boolean        theWNEIsImplemented;                /* Value to return */
  45. long        result;                                /* Value returned */
  46.  
  47.  
  48. Black_ForeColor.red = 0x0000;  Black_ForeColor.green = 0x0000;  Black_ForeColor.blue = 0x0000;  /* Get black color */
  49. White_BackColor.red = 0xFFFF;  White_BackColor.green = 0xFFFF;  White_BackColor.blue = 0xFFFF;  /* Get white color */
  50.  
  51. Has.ColorQD = FALSE;                                /* Init to no color QuickDraw */
  52. Has.FPU = FALSE;                                    /* Init to no floating point chip */
  53. Has.AppleEvents = FALSE;                            /* Whether AppleEvents are available */
  54. Has.AliasMgr = FALSE;                                /* Whether AliasMgr is available */
  55. Has.EditionMgr = FALSE;                                /* Whether EditionMgr is available */
  56. Has.Gestalt = FALSE;                                /* Whether Gestalt is available */
  57. Has.NewStdFile = FALSE;                                /* Whether NewStdFile is available */
  58. Has.PPCToolbox = FALSE;                                /* Whether PPCToolbox is available */
  59. Has.QuickDraw32Bit = FALSE;                            /* Whether 32Bit QuickDraw is available */
  60. InTheForeground = TRUE;                                /* Init to a foreground app */
  61.  
  62. discardError = SysEnvirons(1, &theWorld);            /* Check how old this system is */
  63. if (theWorld.machineType < 0)                        /* Negative means really old */
  64.     {
  65.     theWNEIsImplemented = FALSE;                    /* Really old ROMs, no WNE possible */
  66.     }
  67. else
  68.     {
  69.     theWNEIsImplemented = true;
  70.     Has.ColorQD = theWorld.hasColorQD;                /* Flag for Color QuickDraw being available */
  71.     Has.FPU = theWorld.hasFPU;                        /* Flag for Floating Point Math Chip being available */
  72.     Has.Gestalt = true;
  73.     if (Has.Gestalt)                                /* Do if Gestalt is available */
  74.         {
  75.         discardError = Gestalt(gestaltAliasMgrAttr,&result);
  76.         if ((discardError == 0) && ((result & (0x00000001 << gestaltAliasMgrPresent)) != 0))
  77.             Has.AliasMgr = TRUE;
  78.  
  79.         discardError = Gestalt(gestaltEditionMgrAttr,&result);
  80.         if ((discardError == 0) && ((result & (0x00000001 << gestaltEditionMgrPresent)) != 0))
  81.             Has.EditionMgr = TRUE;
  82.  
  83.         discardError = Gestalt(gestaltAppleEventsAttr,&result);
  84.         if ((discardError == 0) && ((result & (0x00000001 << gestaltAppleEventsPresent)) != 0))
  85.             Has.AppleEvents = TRUE;
  86.  
  87.         discardError = Gestalt(gestaltPPCToolboxAttr,&result);
  88.         if ((discardError == 0) && ((result & (0x00000001 << gestaltPPCToolboxPresent)) != 0))
  89.             Has.PPCToolbox = TRUE;
  90.  
  91.         discardError = Gestalt(gestaltQuickdrawVersion,&result);
  92.         if ((discardError == 0) && ((result & gestalt32BitQD) != 0))
  93.             Has.QuickDraw32Bit = TRUE;
  94.  
  95.         discardError = Gestalt(gestaltStandardFileAttr,&result);
  96.         if ((discardError == 0) && ((result & (0x00000001 << gestaltStandardFile58)) != 0))
  97.             Has.NewStdFile = TRUE;
  98.         }
  99.     }
  100.     
  101. return(theWNEIsImplemented);
  102. }
  103.  
  104. /* ======================================================= */
  105.  
  106. /* Routine: DoOSEvent */
  107. /* Purpose: Handle DoOSEvents */
  108.  
  109. void CmmApplication::DoOSEvent(EventRecord *myEvent)
  110. {
  111.  
  112.  
  113. if (((myEvent->message & osEvtMessageMask) >> 24) == suspendResumeMessage)    /*  See which  */
  114.     {
  115.     if ((myEvent->message & resumeFlag) == 0)    /* Suspend */
  116.         InTheForeground = FALSE;
  117.     else
  118.         InTheForeground = TRUE;
  119.     }
  120. }
  121.  
  122. /* ======================================================= */
  123.  
  124. void CmmApplication::Handle_Extra_User_Event(UserEventPRec theUserEvent)    /* Hook, to handle user events */
  125. {
  126. /* Expected to be overridden by the user code */
  127. }
  128.  
  129. /* ======================================================= */
  130.  
  131. /* Routine: Handle_User_Event */
  132. /* Purpose: Check for user events */
  133.  
  134. void CmmApplication::Handle_User_Event()        /* Check for user events */
  135. {
  136. UserEventRec    TheUserEvent;                    /* The user event */
  137.  
  138.  
  139. GetUserEvent(&TheUserEvent);                    /* Check for any user events */
  140. if (TheUserEvent.ID != UserEvent_None)            /* Only do if we have any */
  141.     {
  142.  
  143.     switch (TheUserEvent.ID)                    /* Key off the Event ID */
  144.         {
  145.         case UserEvent_Open_Window:             /* Open a Window or Modeless dialog */
  146.             switch (TheUserEvent.ID2)            /* Do the appropiate window */
  147.                 {
  148.                 $$Loop Alerts
  149.                 case ResA_$Worksheet.name$:
  150.                     g$Worksheet.name$->BringUpAlert();    /* Open this alert */
  151.                     break;
  152.                 $$EndLoop
  153.                 $$Loop ModalDialogs
  154.                 case ResD_$Worksheet.name$:
  155.                     g$Worksheet.name$->BringUpDialog();    /* Open this modal dialog */
  156.                     break;
  157.                 $$EndLoop
  158.                 $$Loop ModelessDialogs
  159.                 case ResD_$Worksheet.name$:
  160.                     g$Worksheet.name$->Open();        /* Open this modeless dialog */
  161.                     break;
  162.                 $$EndLoop
  163.                 $$Loop Windows
  164.                 case ResW_$Worksheet.name$: 
  165.                     g$Worksheet.name$->Open();        /* Open this window */
  166.                     break;
  167.                 $$EndLoop
  168.                 default:                        /* Handle others */
  169.                     break;
  170.                 }                                /* End of the switch */
  171.             break;
  172.  
  173.         case UserEvent_Close_Window:             /* Close a Window or Modeless dialog */
  174.             switch (TheUserEvent.ID2) {            /* Do the appropiate window */
  175.                 $$Loop ModelessDialogs
  176.                 case ResD_$Worksheet.name$:
  177.                     g$Worksheet.name$->Close(g$Worksheet.name$->theWindow);/* Close this modeless dialog */
  178.                     break;
  179.                 $$EndLoop
  180.                 $$Loop Windows
  181.                 case ResW_$Worksheet.name$: 
  182.                     g$Worksheet.name$->Close((WindowPtr)-1);    /* Close this window */
  183.                     break;
  184.                 $$EndLoop
  185.                 default:                        /* Handle others */
  186.                     break;
  187.                 }                                /* End of the switch */
  188.             break;
  189.  
  190.         default:                                /* Not standard, must be program specific */
  191.             break;
  192.         }                                        /* End of switch */
  193.     }
  194. }
  195.  
  196. /* ======================================================= */
  197.  
  198. /* Routine: DoKeyEvent */
  199. /* Purpose: Handle a key pressed */
  200.  
  201. void CmmApplication::DoKeyEvent(EventRecord *myEvent)        /* Handle key presses */
  202. {
  203. short    charCode;                                    /* Key code */
  204. char    ch;                                            /* Key pressed in Ascii */
  205. long    mResult;                                    /* Menu list and item, if a command key */
  206. short    theMenu,theItem;                            /* Menu list and item, if command key */
  207.  
  208.  
  209. charCode = myEvent->message & charCodeMask;        /* Get the character */
  210. ch = (char)charCode;                            /* Change it to ASCII */
  211.  
  212. if ((myEvent->modifiers & cmdKey) != 0)            /* See if Command key is down */
  213.     {
  214.     gMenus->EnableTheMenus();                    /* Let us in to enable and disable menus*/
  215.     mResult = MenuKey(ch);                        /* See if a menu selection */
  216.     theMenu = HiWord(mResult);                    /* Get the menu list number */
  217.     theItem = LoWord(mResult);                    /* Get the menu item number */
  218.     if (theMenu != 0)                            /* See if a list was selected */
  219.         gMenus->HandleMenuSelection(theMenu,theItem);    /* Do the menu selection */
  220.  
  221.     if (((ch == 'x') || (ch == 'X')) && (theInput != NIL))
  222.         TECut(theInput);                        /* Handle a Cut in a TE area */
  223.     if (((ch == 'c') || (ch == 'C')) && (theInput != NIL))
  224.         TECopy(theInput);                        /* Handle a Copy in a TE area */
  225.     if (((ch == 'v')  ||  (ch == 'V')) && (theInput != NIL))
  226.         TEPaste(theInput);                        /* Handle a Paste in a TE area */
  227.     }
  228. else if (theInput != NIL)
  229.     TEKey(ch,theInput);                            /* Place the normal key stroke */
  230. }
  231.  
  232. /* ======================================================= */
  233.  
  234. /* Routine: DoDiskEvent */
  235. /* Purpose: Handle a diskette inserted */
  236.  
  237. void CmmApplication::DoDiskEvent(EventRecord *myEvent)                            /* Handle disk inserted */
  238. {
  239. short    theError;                                    /* Error returned from mount */
  240.  
  241.  
  242. if (HiWord(myEvent->message) != noErr)                /* See if a diskette mount error */
  243.     {
  244.     myEvent->where.h = ((screenRect.bounds.right - screenRect.bounds.left) / 2) - (304 / 2);/* Center horz */
  245.     myEvent->where.v = ((screenRect.bounds.bottom - screenRect.bounds.top) / 3) - (104 / 2);/* Top 3ed vertically */
  246.     InitCursor();                                    /* Make sure it has an arrow cursor */
  247.     theError = DIBadMount(myEvent->where, myEvent->message);/* Let the OS handle the diskette */
  248.     }
  249. }
  250.  
  251. /* ======================================================= */
  252.  
  253. /* Routine: DoZoom */
  254. /* Purpose: Handle a window zoom */
  255.  
  256. Boolean CmmApplication::DoZoom(WindowPtr whichWindow,short code,EventRecord *myEvent)/* Handle a window zoom */
  257. {
  258. Rect    OldRect;                                    /* Window rect before the zoom */
  259. Point    myPt;                                        /* Point for tracking zoom box */
  260. long    theRefCon;                                    /* Refcon with layer masked off */
  261. Boolean        HandledEvent;
  262.  
  263.  
  264. HandledEvent = false;
  265. if (!Doing_MovableModal)                            /* Select proper window */
  266.     {
  267.     if (whichWindow != NIL)                            /* See if we have a legal window */
  268.         {
  269.         SetPort(whichWindow);                        /* Get ready to draw in this window */
  270.  
  271.         myPt = myEvent->where;                        /* Get mouse position */
  272.         GlobalToLocal(&myPt);                        /* Make it relative */
  273.  
  274.         OldRect = whichWindow->portRect;            /* Save the rect before resizing */
  275.  
  276.         if (TrackBox(whichWindow, myPt, code))        /* Zoom it */
  277.             {
  278.             ZoomWindow(whichWindow, code, TRUE);    /* Resize to result */
  279.             EraseRect(&whichWindow->portRect);        /* Make sure we update the whole window effectively */
  280.             InvalRect(&whichWindow->portRect);        /* Tell ourselves to update, redraw, the window contents */
  281.             theRefCon = GetWRefCon(whichWindow);    /* Get the refcon */
  282.             theRefCon = theRefCon & 0x00FFFFFF;        /* Mask the layer out */
  283.             HandledEvent = true;
  284.             switch (theRefCon)                        /* Do the appropiate window */
  285.                 {
  286.                 $$Loop Windows
  287.                 $$if Window.Zoomable
  288.                 case ResW_$Worksheet.name$:
  289.                     g$Worksheet.name$->Resized(&OldRect, whichWindow);    /* Resized this window */
  290.                     break;
  291.                 $$endif
  292.                 $$EndLoop
  293.  
  294.                 default:                            /* Handle others */
  295.                     HandledEvent = false;
  296.                     break;
  297.                 }
  298.             Mk_HiliteWindow(whichWindow);            /* Hilite it again */
  299.             }
  300.         }
  301.     }
  302. return(HandledEvent);
  303. }
  304.  
  305. /* ======================================================= */
  306.  
  307. /* Routine: DoGrow */
  308. /* Purpose: Handle a window resize */
  309.  
  310. Boolean CmmApplication::DoGrow(WindowPtr whichWindow,EventRecord *myEvent)
  311. {
  312. Rect    OldRect;                                    /* Window rect before the grow */
  313. Point    myPt;                                        /* Point for tracking grow box */
  314. Rect    GrowRect;                                    /* Set the grow bounds */
  315. long    mResult;                                    /* Result from the grow */
  316. long    theRefCon;                                    /* Refcon with layer masked off */
  317. Boolean        HandledEvent;
  318.  
  319.  
  320. HandledEvent = false;
  321. if (!Doing_MovableModal)                            /* Select proper window */
  322.     {
  323.     if (whichWindow != nil)                            /* See if we have a legal window */
  324.         {
  325.         SetPort(whichWindow);                        /* Get ready to draw in this window */
  326.  
  327.         myPt = myEvent->where;                        /* Get mouse position */
  328.         GlobalToLocal(&myPt);                        /* Make it relative */
  329.  
  330.         OldRect = whichWindow->portRect;            /* Save the rect before resizing */
  331.  
  332.         SetRect(&GrowRect, 24, 4,4096L,  4096L);/* l,t,r,b */
  333.         mResult = GrowWindow(whichWindow, myEvent->where, &GrowRect);/* Grow it */
  334.         SizeWindow(whichWindow, LoWord(mResult), HiWord(mResult), true);/* Resize to result */
  335.  
  336.         HandledEvent = true;
  337.         theRefCon = GetWRefCon(whichWindow);        /* Get the refcon */
  338.         theRefCon = theRefCon & 0x00FFFFFF;            /* Mask the layer out */
  339.  
  340.         switch (theRefCon)                            /* Do the appropiate window */
  341.             {
  342.             $$Loop Windows
  343.             $$if Window.Resizable
  344.             case ResW_$Worksheet.name$:
  345.                 g$Worksheet.name$->Resized(&OldRect, whichWindow);    /* Resized this window */
  346.                 break;
  347.             $$endif
  348.             $$EndLoop
  349.  
  350.             default:                                /* allow other buttons, trap for debug */
  351.                 HandledEvent = false;
  352.                 break;                                /* end of otherwise */
  353.             }                                        /* end of switch */
  354.  
  355.         SetPort(whichWindow);                        /* Get ready to draw in this window */
  356.  
  357.         myPt.h = whichWindow->portRect.right - whichWindow->portRect.left;/* Local right edge */
  358.         myPt.v = whichWindow->portRect.bottom - whichWindow->portRect.top;/* Local bottom edge */
  359.  
  360.         SetRect(&GrowRect, 0, OldRect.bottom - 15, OldRect.right + 15, OldRect.bottom + 15);/* Position for horz scrollbar area */
  361.         EraseRect(&GrowRect);                        /* Erase old area */
  362.         InvalRect(&GrowRect);                        /* Flag us to update it */
  363.  
  364.         SetRect(&GrowRect, OldRect.right - 15, 0, OldRect.right + 15, OldRect.bottom + 15);/* Position for vert scrollbar area */
  365.         EraseRect(&GrowRect);                        /* Erase old area */
  366.         InvalRect(&GrowRect);                        /* Flag us to update it */
  367.  
  368.         DrawGrowIcon(whichWindow);                    /* Draw the grow Icon again */
  369.         }
  370.     }
  371. return(HandledEvent);
  372. }
  373.  
  374. /* ======================================================= */
  375.  
  376. /* Routine: DoDrag */
  377. /* Purpose: Drag a window around */
  378.  
  379. Boolean CmmApplication::DoDrag(WindowPtr whichWindow,EventRecord *myEvent)
  380. {
  381. Rect    OldRect;                                /* Window rect before the drag */
  382. Rect    tempRect;                                /* temporary rect */
  383. long    theRefCon;                                /* Refcon with layer masked off */
  384. Boolean        HandledEvent;
  385.  
  386.  
  387. HandledEvent = false;
  388. if ((!Doing_MovableModal) || (Doing_MovableModal && (whichWindow == FrontWindow())))/* See if OK to drag */
  389.     {
  390.     OldRect = whichWindow->portRect;            /* Save the rect before resizing */
  391.  
  392.     if (!Doing_MovableModal)                    /* Select proper window */
  393.         Mk_HiliteWindow(whichWindow);
  394.     SetRect(&tempRect, -32000, -32000, 32000, 32000);
  395.     Mk_DragWindow(whichWindow, myEvent->where, tempRect);/* Drag the window */
  396.  
  397.     HandledEvent = true;
  398.     theRefCon = GetWRefCon(whichWindow);        /* Get the refcon */
  399.     theRefCon = theRefCon & 0x00FFFFFF;            /* Mask the layer out */
  400.     switch (theRefCon)                            /* Do the appropiate window */
  401.         {
  402.         $$Loop ModelessDialogs
  403.         case ResD_$Worksheet.name$:
  404.             g$Worksheet.name$->Moved(&OldRect, whichWindow);    /* Moved this modeless dialog */
  405.             break;
  406.         $$EndLoop
  407.         $$Loop Windows
  408.         case ResW_$Worksheet.name$:
  409.             g$Worksheet.name$->Moved(&OldRect, whichWindow);    /* Moved this window */
  410.             break;
  411.         $$EndLoop
  412.  
  413.         default:                                /* allow other buttons, trap for debug */
  414.             HandledEvent = false;
  415.             break;                                /* end of otherwise */
  416.         }                                        /* end of switch */
  417.     }
  418. return(HandledEvent);
  419. }
  420.  
  421. /* ======================================================= */
  422.  
  423. /* Routine: DoGoAway */
  424. /* Purpose: Close a window */
  425.  
  426. Boolean CmmApplication::DoGoAway(WindowPtr whichWindow,EventRecord *myEvent)
  427. {
  428. long        theRefCon;                            /* Refcon with layer masked off */
  429. Boolean        HandledEvent;
  430.  
  431.  
  432. HandledEvent = false;
  433. if (!Doing_MovableModal)                        /* Select proper window */
  434.     {
  435.     if (TrackGoAway(whichWindow,myEvent->where))    /* See if mouse released in GoAway box */
  436.         {
  437.         HandledEvent = true;
  438.         theRefCon = GetWRefCon(whichWindow);    /* Get the refcon */
  439.         theRefCon = theRefCon & 0x00FFFFFF;        /* Mask the layer out */
  440.         switch (theRefCon)                        /* Do the appropiate window */
  441.             {
  442.             $$Loop ModelessDialogs
  443.             $$if ModelessDialog.GoAway
  444.             case ResD_$Worksheet.name$:
  445.                 g$Worksheet.name$->Close(whichWindow);    /* Close this modeless dialog */
  446.                 break;
  447.             $$endif
  448.             $$EndLoop
  449.             $$Loop Windows
  450.             $$if Window.GoAway
  451.             case ResW_$Worksheet.name$:
  452.                 g$Worksheet.name$->Close(whichWindow);    /* Close this window */
  453.                 break;
  454.             $$endif
  455.             $$EndLoop
  456.  
  457.             default:                            /* allow other buttons, trap for debug */
  458.                 HandledEvent = false;
  459.                 break;                            /* end of otherwise */
  460.             }                                    /* end of switch */
  461.         }
  462.     }
  463. return(HandledEvent);
  464. }
  465.  
  466. /* ======================================================= */
  467.  
  468. /* Routine: DoInContent */
  469. /* Purpose: Pressed in the content area */
  470.  
  471. Boolean CmmApplication::DoInContent(WindowPtr whichWindow,EventRecord *myEvent)
  472. {
  473. long        theRefCon;                            /* Refcon with layer masked off */
  474. Boolean        HandledEvent;
  475.  
  476.  
  477. HandledEvent = false;
  478. if (!Doing_MovableModal)                        /* Select proper window */
  479.     {
  480.     if (Mk_Is_FrontWindow(whichWindow) == false)    /* See if already selected or not, in front if selected */
  481.         Mk_HiliteWindow(whichWindow);
  482.     else
  483.         {
  484.         SetPort(whichWindow);                    /* Get ready to draw in this window */
  485.         Mk_HiliteWindow(whichWindow);
  486.         theRefCon = GetWRefCon(whichWindow);    /* Get the refcon */
  487.         theRefCon = theRefCon & 0x00FFFFFF;        /* Mask the layer out */
  488.         HandledEvent = true;
  489.  
  490.         switch (theRefCon)                        /* Do the appropiate window */
  491.             {
  492.             $$Loop Windows
  493.             case ResW_$Worksheet.name$:
  494.                 g$Worksheet.name$->HandleEvent(myEvent);    /* Handle this window */
  495.                 break;
  496.             $$EndLoop
  497.  
  498.             default:                            /* allow other buttons, trap for debug */
  499.                 HandledEvent = false;
  500.                 break;                            /* end of otherwise */
  501.             }                                    /* end of switch */
  502.         }
  503.     }
  504. return(HandledEvent);
  505. }
  506.  
  507. /* ======================================================= */
  508.  
  509. /* Routine: DoUpdate */
  510. /* Purpose: Got an update event */
  511.  
  512. Boolean CmmApplication::DoUpdate(EventRecord *myEvent)
  513. {
  514. WindowPtr    whichWindow;                        /* See which window for event */
  515. long        theRefCon;                            /* Refcon with layer masked off */
  516. Boolean        HandledEvent;
  517.  
  518.  
  519. whichWindow = (WindowPtr)myEvent->message;        /* Get the window the update is for */
  520. HandledEvent = true;
  521.  
  522. BeginUpdate(whichWindow);                        /* Set the clipping to the update area */
  523. theRefCon = GetWRefCon(whichWindow);            /* Get the refcon */
  524. theRefCon = theRefCon & 0x00FFFFFF;                /* Mask the layer out */
  525. switch (theRefCon)                                /* Do the appropiate window */
  526.     {
  527.     $$Loop Windows
  528.     case ResW_$Worksheet.name$:
  529.         g$Worksheet.name$->Update(whichWindow);        /* Update this window */
  530.         break;
  531.     $$EndLoop
  532.  
  533.     default:                                    /* allow other buttons, trap for debug */
  534.         HandledEvent = false;
  535.         break;                                    /* end of otherwise */
  536.     }                                            /* end of switch */
  537. EndUpdate(whichWindow);                            /* Return to normal clipping area */
  538. return(HandledEvent);
  539. }
  540.  
  541. /* ======================================================= */
  542.  
  543. /* Routine: CmmApplication.DoActivate */
  544. /* Purpose: Got an activate or deactivate event */
  545.  
  546. Boolean CmmApplication::DoActivate(EventRecord *myEvent)
  547. {
  548. WindowPtr    whichWindow;                        /* See which window for event */
  549. Boolean        Do_An_Activate;                        /* Flag to pass */
  550. Boolean        HandledEvent;
  551. long        theRefCon;                            /* Refcon with layer masked off */
  552.  
  553.  
  554. whichWindow = (WindowPtr)myEvent->message;        /* Get the window the update is for */
  555. HandledEvent = true;
  556.  
  557. Do_An_Activate =  ((myEvent->modifiers & 0x0001) != 0);/* Make sure it is Activate and not DeActivate */
  558. theRefCon = GetWRefCon(whichWindow);            /* Get the refcon */
  559. theRefCon = theRefCon & 0x00FFFFFF;                /* Mask the layer out */
  560. switch (theRefCon)                                /* Do the appropiate window */
  561.     {
  562.     $$Loop Windows
  563.     case ResW_$Worksheet.name$:
  564.         g$Worksheet.name$->Activate(whichWindow,Do_An_Activate);    /* Activate or deactivate this window */
  565.         break;
  566.     $$EndLoop
  567.  
  568.     default:                                    /* allow other buttons, trap for debug */
  569.         HandledEvent = false;
  570.         break;                                    /* end of otherwise */
  571.     }                                            /* end of switch */
  572. return(HandledEvent);
  573. }
  574.  
  575. /* ======================================================= */
  576.  
  577. /* Routine: CmmApplication.DoHighLevelEvent */
  578. /* Purpose: Got a High Level event */
  579.  
  580. void CmmApplication::DoHighLevelEvent(EventRecord *myEvent)
  581. {
  582. OSErr    err;                                    /* Error */
  583.  
  584.  
  585. err = AEProcessAppleEvent(myEvent);                /* Go to the processor of apple events */
  586. }
  587.  
  588. /* ======================================================= */
  589.  
  590. /* Routine: CmmApplication.InitExtras */
  591. /* Purpose: Extra Initialization for the application */
  592.  
  593. void CmmApplication::InitExtras()
  594. {
  595. /* Expected to be overridden by the user code */
  596. }
  597.  
  598. /* ======================================================= */
  599.  
  600. /* Routine: CmmApplication.OpenSplashWindow */
  601. /* Purpose: Open spash or personalization window */
  602.  
  603. void CmmApplication::OpenSplashWindow()
  604. {
  605. /* Expected to be overridden by the user code */
  606. }
  607.  
  608. /* ======================================================= */
  609.  
  610. /* Routine: CmmApplication.CloseSplashWindow */
  611. /* Purpose: Close spash or personalization window */
  612.  
  613. void CmmApplication::CloseSplashWindow()
  614. {
  615. /* Expected to be overridden by the user code */
  616. }
  617.  
  618. /* ======================================================= */
  619.  
  620. /* Routine: CmmApplication.Init */
  621. /* Purpose: Initialize the application */
  622.  
  623. void CmmApplication::Init()                        /* Initialize the application */
  624. {
  625.  
  626.  
  627. MoreMasters();                                    /* This reserves space for more handles */
  628. MaxApplZone();                                    /* Give us room for memory allocation */
  629. InitGraf(&QDPort);                                /* Quickdraw Init */
  630. InitFonts();                                    /* Font manager init */
  631. InitWindows();                                    /* Window manager init */
  632. InitMenus();                                    /* Menu manager init */
  633. TEInit();                                        /* Text edit init */
  634. InitDialogs(nil);                                /* Dialog manager */
  635.  
  636. FlushEvents(everyEvent,0);                        /* Clear out all events */
  637. InitCursor();                                    /* Make an arrow cursor */
  638.  
  639. doneFlag = FALSE;                                /* Do not exit program yet */
  640.  
  641. gMenus = new CMenus;                            /* Allocate the menu handler object */
  642. gMenus->Init();                                    /* Init the object and the menu bar */
  643.  
  644. gPreferences = new CPreferences;                /* Allocate the preferences object */
  645. gPreferences->Init();                            /* Init the object */
  646.  
  647. gFiles = new CFiles;                            /* Allocate the file handler object */
  648. gFiles->Init();                                    /* Init the object */
  649.  
  650. gPrinting = new CPrinting;                        /* Allocate the printing handler object */
  651. gPrinting->Init();                                /* Init the object */
  652.  
  653. this->OpenSplashWindow();                        /* Open spash or personalization window */
  654.  
  655. theInput = nil;                                    /* Init to no text edit selection active */
  656.  
  657. SleepValue = 40;                                /* Set sleep value */
  658. WNE = this->IsWNEIsImplemented();                /* See if WaitNextEvent is available */
  659.  
  660. UserEventList = nil;                            /* No user events yet */
  661.  
  662. cursorRgn = NewRgn();                            /* Cursor region for WaitNextEvent */
  663.  
  664. if (Has.AppleEvents)                            /* See if AppleEvents are available */
  665.     InitAppleEvents();                            /* Initialize for AppleEvents */
  666.  
  667. gCurrentAlert = nil;
  668. $$Loop Alerts
  669. g$Worksheet.name$ = new CA$Worksheet.name$;            /* Allocate the alert object */
  670. g$Worksheet.name$->Init();
  671. $$EndLoop
  672. $$Loop ModalDialogs
  673. g$Worksheet.name$ = new CD$Worksheet.name$;            /* Allocate the Modal Dialog object */
  674. g$Worksheet.name$->Init();
  675. $$EndLoop
  676. $$Loop ModelessDialogs
  677. g$Worksheet.name$ = new CMD$Worksheet.name$;        /* Allocate the Modeless Dialog object */
  678. g$Worksheet.name$->Init();
  679. $$EndLoop
  680. $$Loop Windows
  681. g$Worksheet.name$ = new CW$Worksheet.name$;            /* Allocate the window object */
  682. g$Worksheet.name$->Init();                            /* Initialize Window, $Worksheet.FullName$ */
  683. $$EndLoop
  684.  
  685. Mk_ClearLayers();                                /* Init layer array */
  686. Doing_MovableModal = false;                        /* Not currently doing a movable modal */
  687.  
  688. this->InitExtras();                                /* HOOK, Do extra user initialization */
  689.  
  690. gPreferences->GetPreferences();                    /* Get preferences */
  691.  
  692. $$Loop Alerts
  693. $$if Worksheet.OpenAtStartup
  694. g$Worksheet.name$->BringUpAlert();                /* Open at program start, $Worksheet.FullName$ */
  695. $$endif
  696. $$EndLoop
  697. $$Loop ModalDialogs
  698. $$if Worksheet.OpenAtStartup
  699. g$Worksheet.name$->BringUpDialog();                /* Open at program start, $Worksheet.FullName$ */
  700. $$endif
  701. $$EndLoop
  702. $$Loop ModelessDialogs
  703. $$if Worksheet.OpenAtStartup
  704. g$Worksheet.name$->Open();                        /* Open at program start, $Worksheet.FullName$ */
  705. $$endif
  706. $$EndLoop
  707. $$Loop Windows
  708. $$if Worksheet.OpenAtStartup
  709. g$Worksheet.name$->Open();                        /* Open at program start, $Worksheet.FullName$ */
  710. $$endif
  711. $$EndLoop
  712.  
  713. this->CloseSplashWindow();                        /* Close spash or personalization window */
  714. }
  715.  
  716. /* ======================================================= */
  717.  
  718. /* Routine: CmmApplication.TopOfLoop */
  719. /* Purpose: HOOK at top of main event loop */
  720.  
  721. void CmmApplication::TopOfLoop()
  722. {
  723. /* Expected to be overridden by the user code */
  724. }
  725.  
  726. /* ======================================================= */
  727.  
  728. /* Routine: CmmApplication.FilterEvent */
  729. /* Purpose: HOOK, Allow user to filter all events before the main loop handles them */
  730. /* Set  DoIt  to TRUE to let the main loop handle the event. */
  731.  
  732. void CmmApplication::FilterEvent(Boolean *DoIt, EventRecord *myEvent)
  733. {
  734.  
  735.  
  736. *DoIt = TRUE;                                        /* Let the main loop handle it */
  737. if (myEvent->what == 0)                             /* Handle a NULL event*/
  738.     *DoIt = FALSE;                                    /* ...Tell the main loop to skip it*/
  739. }
  740.  
  741. /* ======================================================= */
  742.  
  743. /* Routine: CmmApplication.InitAppleEvents */
  744. /* Purpose: Init AppleEvent handlers. */
  745.  
  746. void CmmApplication::InitAppleEvents(void)
  747. {
  748. OSErr    err;                                        /* Error */
  749. AEEventHandlerUPP        theRoutine;
  750.  
  751.  
  752. theRoutine = NewAEEventHandlerProc(DispatchAppleEvent);
  753. err = AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
  754.     theRoutine,kAEOpenApplication,false);
  755. err = AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
  756.     theRoutine,kAEOpenDocuments,false);
  757. err = AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,
  758.     theRoutine,kAEPrintDocuments,false);
  759. err = AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
  760.     theRoutine,kAEQuitApplication,false);
  761.  
  762. //err = AEObjectInit();                                /* Init the AE Object code */
  763.  
  764. gSelfPSN.highLongOfPSN = 0;                        /* Init to the current process ID */
  765. gSelfPSN.lowLongOfPSN = kCurrentProcess;
  766. err = AECreateDesc(typeProcessSerialNumber,(Ptr)&gSelfPSN,sizeof(ProcessSerialNumber),&gSelfAddress);
  767. }
  768.  
  769. /* ======================================================= */
  770.  
  771. /* Routine: CmmApplication.Run */
  772. /* Purpose: Run the application */
  773.  
  774. void CmmApplication::Run()                            /* Run the application */
  775. {
  776. Boolean        HandledEvent;
  777.  
  778.  
  779. do
  780.     {
  781.     this->TopOfLoop();                                /* HOOK at top of main event loop */
  782.  
  783.     this->Handle_User_Event();                        /* Check for user events */
  784.  
  785.     if (theInput != NIL)                            /* See if a TE is active */
  786.         TEIdle(theInput);                            /* Blink the cursor if everything is ok */
  787.  
  788.     if (WNE)                                        /* See if do the MultiFinder way */
  789.         DoIt = WaitNextEvent(everyEvent, &myEvent, SleepValue, cursorRgn);/* Wait for an event */
  790.     else
  791.         {
  792.         SystemTask();                                /* For support of desk accessories */
  793.         DoIt = GetNextEvent(everyEvent, &myEvent);    /* See if an event is ready */
  794.         }
  795.  
  796.     this->FilterEvent(&DoIt,&myEvent);                /* HOOK, Let us at the event first */
  797.  
  798.     if (DoIt)                                        /* If event then... */
  799.         {
  800.         Is_A_Dialog = IsDialogEvent(&myEvent);        /* See if a modeless dialog event */
  801.         if (Is_A_Dialog)                            /* Handle a dialog event */
  802.             {
  803.              if (myEvent.what == updateEvt)            /* Handle the update of a Modeless Dialog */
  804.                 {
  805.                 whichWindow = (WindowPtr)myEvent.message; /* Get the window the update is for */
  806.                 BeginUpdate(whichWindow);            /* Set update clipping area */
  807.                 $$Loop ModelessDialogs
  808.                 g$Worksheet.name$->Update(whichWindow);    /* Update Modeless Dialog, $Worksheet.FullName$ */
  809.                 $$EndLoop
  810.                 EndUpdate(whichWindow);                /* Return to normal clipping area */
  811.                 }
  812.             else
  813.                 {
  814.                 DoTheModelessEvent = TRUE;            /* Go ahead and do it so far */
  815.  
  816.                 if (myEvent.what == keyDown)         /* Check the key down, for a command key event */
  817.                     {
  818.                     CmdDown = ((myEvent.modifiers / cmdKey) & 1);/* Get the command key state */
  819.                     charCode = myEvent.message & charCodeMask;/* Get the character */
  820.                     ch = (char)charCode;            /* Change it to ASCII */
  821.  
  822.                     if ((charCode == 13) || (charCode == 0x03))/* CR or Enter */
  823.                         DoTheModelessEvent = TRUE;    /* Handle the default selection */
  824.  
  825.                     if (CmdDown != 0)                /* Handle if the command key was down */
  826.                         {
  827.                         gMenus->EnableTheMenus();    /* Let us in to enable and disable menus*/
  828.                         mResult = MenuKey(ch);        /* See if a menu selection */
  829.                         theMenu = HiWord(mResult);    /* Get the menu list number */
  830.                         theItem = LoWord(mResult);    /* Get the menu item number */
  831.                         if (theMenu != 0)              /* See if a list was selected */
  832.                             gMenus->HandleMenuSelection(theMenu,theItem);    /* Do the menu selection */
  833.  
  834.                         whichWindow = FrontWindow(); /* Get the front window */
  835.                         if ((ch == 'x') || (ch == 'X'))    /* Handle a CUT */
  836.                             DlgCut(whichWindow);
  837.                         if ((ch == 'c') || (ch == 'C'))    /* Handle a COPY */
  838.                             DlgCopy(whichWindow);
  839.                         if ((ch == 'v') || (ch == 'V'))    /* Handle a PASTE */
  840.                             DlgPaste(whichWindow);
  841.  
  842.                         DoTheModelessEvent = FALSE;/* We handled the command key */
  843.                         }
  844.                     }
  845.  
  846.                 if (DoTheModelessEvent)             /* Do we handle it? */
  847.                     {
  848.                     if ((DialogSelect(&myEvent, &whichWindow, &itemHit)) || (myEvent.what == mouseDown) || (myEvent.what == keyDown)) /* Ck if do it */
  849.                         {
  850.                         $$Loop ModelessDialogs
  851.                         g$Worksheet.name$->HandleEvent(&myEvent,whichWindow,itemHit);    /* Handle the Modeless Dialog, $Worksheet.FullName$ */
  852.                         $$EndLoop
  853.                         }
  854.                     }
  855.                 }
  856.             }
  857.         else
  858.             {
  859.  
  860.             switch (myEvent.what)                    /* Decide type of event */
  861.                 {
  862.                 case mouseDown :                /* Mouse button pressed */
  863.                     code = FindWindow(myEvent.where, &whichWindow);/* Get which window the event happened in */
  864.  
  865.                     switch (code)                /* Decide type of event again */
  866.                         {
  867.                         case inMenuBar :        /* In the menubar */
  868.                             gMenus->EnableTheMenus();            /* Let us in to enable and disable menus*/
  869.                             mResult = MenuSelect(myEvent.where);/* Do menu selection */
  870.                             theMenu = HiWord(mResult);            /* Get the menu list number */
  871.                             theItem = LoWord(mResult);            /* Get the menu list item number */
  872.                             gMenus->HandleMenuSelection(theMenu,theItem);    /* Do the menu selection */
  873.                             break;
  874.  
  875.                         case inDrag :        /* In window drag area */
  876.                             HandledEvent = this->DoDrag(whichWindow,&myEvent);
  877.                             break;
  878.  
  879.                         case inGrow :        /* In window grow area */
  880.                             HandledEvent = this->DoGrow(whichWindow,&myEvent);
  881.                             break;
  882.  
  883.                         case inZoomIn :        /* In window zoom area */
  884.                         case inZoomOut :
  885.                             HandledEvent = this->DoZoom(whichWindow,code,&myEvent);
  886.                             break;
  887.  
  888.                         case inGoAway :        /* In window goaway area */
  889.                             HandledEvent = this->DoGoAway(whichWindow,&myEvent);
  890.                             break;
  891.  
  892.                         case inContent :    /* In window  contents */
  893.                             HandledEvent = this->DoInContent(whichWindow,&myEvent);
  894.                             break;
  895.  
  896.                         case inSysWindow :    /* See if a DA selection */
  897.                             SystemClick(&myEvent, whichWindow);    /* Let other programs in */
  898.                             break;
  899.  
  900.                         default:            /* Handle others */
  901.                             break;            /* End of otherwise */
  902.                         }                    /* End of code case */
  903.                     break;                    /* End of MouseDown */
  904.  
  905.                 case keyDown:                /* Handle key inputs */
  906.                 case autoKey:                /* and auto repeats */
  907.                     this->DoKeyEvent(&myEvent);
  908.                     break;
  909.  
  910.                 case updateEvt :            /* Update event for a window */
  911.                     HandledEvent = this->DoUpdate(&myEvent);
  912.                     break;
  913.  
  914.                 case diskEvt :                /* Disk inserted event */
  915.                     this->DoDiskEvent(&myEvent);
  916.                     break;
  917.  
  918.                 case activateEvt :            /* Window activated event */
  919.                     HandledEvent = this->DoActivate(&myEvent);
  920.                     break;
  921.  
  922.                 case osEvt:                    /* OS event */
  923.                     this->DoOSEvent(&myEvent);
  924.                     break;
  925.  
  926.                 case kHighLevelEvent:        /* High Level event */
  927.                     if (Has.AppleEvents)    /* See if has Apple Events */
  928.                         this->DoHighLevelEvent(&myEvent);    /* Do AppleEvents */
  929.                     break;
  930.  
  931.                 default:                    /* Used for debugging, to see what other events are coming in */
  932.                     break;                    /* End of otherwise */
  933.  
  934.                 }                            /* End of case */
  935.  
  936.             }
  937.         }
  938.     else
  939.         {
  940.         whichWindow = FrontWindow();        /* Get the current front window */
  941.         if (whichWindow != NIL)                /* See if we have a window */
  942.             {
  943.             thePeeked = (WindowPeek)whichWindow;        /* Peek inside, look for dialog */
  944.             if (thePeeked->windowKind == dialogKind)    /* DialogSelect will crash if no dialogs */
  945.                 {
  946.                 if (DialogSelect(&myEvent, &whichWindow, &itemHit))/* Blink cursor in modeless TEs */
  947.                     {
  948.                     }
  949.                 }
  950.             }
  951.         }
  952.     }                                        /* end of while */
  953. while (!doneFlag);                            /* End of the event loop */
  954. }
  955.  
  956. /* ======================================================= */
  957.  
  958. /* Routine: CmmApplication.ExitExtras */
  959. /* Purpose: Exit the application */
  960.  
  961. void CmmApplication::ExitExtras()                            /* Exit the application */
  962. {
  963. /* Expected to be overridden by the user code */
  964. }
  965.  
  966. /* ======================================================= */
  967.  
  968. /* Routine: CmmApplication.Exit */
  969. /* Purpose: Exit the application */
  970.  
  971. void CmmApplication::Exit()                            /* Exit the application */
  972. {
  973.  
  974.  
  975. $$Loop ModelessDialogs
  976. g$Worksheet.name$->Close(g$Worksheet.name$->theWindow);    /* Close this modeless dialog */
  977. $$EndLoop
  978. $$Loop Windows
  979. g$Worksheet.name$->Close((WindowPtr)-1);    /* Close this window */
  980. $$EndLoop
  981.  
  982. gPreferences->SetPreferences();                    /* Set default preferences */
  983.  
  984. $$Loop Alerts
  985. delete g$Worksheet.name$;                        /* Deallocate the alert object */
  986. $$EndLoop
  987. $$Loop ModalDialogs
  988. delete g$Worksheet.name$;                        /* Deallocate the Modal object */
  989. $$EndLoop
  990. $$Loop ModelessDialogs
  991. delete g$Worksheet.name$;                        /* Deallocate the Modeless object */
  992. $$EndLoop
  993. $$Loop Windows
  994. delete g$Worksheet.name$;                        /* Deallocate the window object */
  995. $$EndLoop
  996. ExitExtras();                                        /* HOOK, Do extra user exit code */
  997. }
  998.  
  999. /* ======================================================= */
  1000. /* ======================================================= */
  1001.  
  1002.  void main()                                    /* Start of main body */
  1003. {
  1004.  
  1005.  
  1006. gApplication = new CApplication;                /* Allocate the application object */
  1007.  
  1008. gApplication->Init();                            /* Init the object and the application */
  1009.  
  1010. gApplication->Run();                            /* Run the application */
  1011.  
  1012. gApplication->Exit();                            /* Exit the application */
  1013.  
  1014. }                                                /* end of main */
  1015. $$CloseFile
  1016.  
  1017.